You can define functions and subroutines to organize and structure scripts for easier maintenance. You may want to create subroutines and functions for commonly performed actions such as logging in to an application, or for generating data, such as unique passwords.
You can create subroutines or functions in individual scripts, but they are most often created in scripts called by other scripts. You can also create multiple functions or subroutines in the called script.
If you store functions or subroutines in a script called by other scripts, the script name must conform to the following rules:
Tip: As you work with a script, you can view a list of variables, functions, and subroutines defined in the script in the Definition field at the top of the Script pane. To go directly to the script line where a function or subroutine is defined, select it in the Definition field or right-click a script line that references it and choose Go To Definition. See Finding variable, function, and subroutine definitions in scripts. You can then click the Go Back and Go Forward toolbar buttons to navigate back and forward through the areas you worked in.
Use the Sub...End Sub statements to declare subroutines. Subroutines do not return values. For example, a=Sub1 results in an error because Sub1 cannot return a value.
Use the following format to declare subroutines:
Sub Name (arg1, arg2,…)
[statements]
End Sub
Use the Function...End Function statements to declare functions. Functions can accept an unlimited number of arguments and must always return a value. You can define the return value to be whatever you need. The expression is evaluated and results are returned to the calling statement or script. Functions are generally assigned to a variable or printed to the Output pane.
Use the following format to declare functions:
Function Name (arg1, arg2,…)
[statements]
Return result
End Function
Returning values from functions
To return a value from a function, use the Return keyword. You do not have to assign the value to the function name. The following examples show how the return keyword is used.
Function myFuncReturnCheck(value)
Return value
End Function
retrieved = myFuncReturnCheck("string")
PrintLn(retrieved)
Calling functions and subroutines within a script
To call a function or subroutine in the same script, refer to it by name. For example, you create a function named timeWithDashes. To use it, add the following line to a script:
PrintLn("Date and time script ran:" + timeWithDashes())
Calling functions and subroutines from another script
To call a function or subroutine from another script, you must first call that script using the Script.CallScript statement. You do not have to immediately use the function or subroutine. To run the function or subroutine, enter the called script name, a period, then the function or subroutine name:
ScriptName.FunctionName/SubroutineName
Note: To avoid syntax errors, the called script name must start with an alphabetic character, cannot contain spaces, and can only contain alphanumeric or underscore characters.
For example, you created a script named Utilities that stores subroutines. Add the following lines to the script to call the WebLogin subroutine:
Script.CallScript("Utilities")
Utilities.WebLogin()